package com.ywk.base.toolClass.scriptEngine.google.factory;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.Options;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * AviatorEvaluator求值器实例单例获取
 *
 * @author eric02.li
 */
public class AviatorEvaluatorFactory {
    private AviatorEvaluatorFactory() {
        throw new IllegalStateException("Utility class");
    }
    private static final Map<String, AviatorEvaluatorInstance> instanceMap = new ConcurrentHashMap<>();

    public static AviatorEvaluatorInstance getInstance(String cacheKey) {
        return instanceMap.computeIfAbsent(cacheKey, (v) -> {
            AviatorEvaluatorInstance instance = AviatorEvaluator.getInstance();
            instance.setOption(Options.ALWAYS_PARSE_FLOATING_POINT_NUMBER_INTO_DECIMAL, true);
            instance.setOption(Options.USE_USER_ENV_AS_TOP_ENV_DIRECTLY, false);

            return instance;
        });
    }
}
